package leetcode;

/**
 * @Classname RemoveNthFromEnd_0019
 * @Author happytaohaha
 * @Date 2020/3/11
 * @Description 删除链表的倒数第N个节点   双节点先让第一个节点走n个
 */
public class RemoveNthFromEnd_0019 {

    public ListNode removeNthFromEnd(ListNode head, int n) {
        if(n<=0){
            return head;
        }
        ListNode p = head;
        ListNode q = head;
        while (n > 0 && p!=null) {
            p = p.next;
            n--;
        }
        if(p==null){
            return head.next;
        }
        while (p.next != null) {
            p = p.next;
            q = q.next;
        }
        q.next =q.next.next;
        return head;
    }
}
